package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * private Long dishId;
     * private Long setmealId;
     * private String dishFlavor;
     *
     * @param kkk
     */
    @Override
    public void addToShopCart(ShoppingCartDTO kkk) {
        ShoppingCart kk = new ShoppingCart();
        BeanUtils.copyProperties(kkk,kk);
        kk.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> kks = shoppingCartMapper.list(kk);
//        ShoppingCart thatOne = shoppingCartMapper.getThatOne(kk);
        System.out.println(kks);

        if(kks != null && kks.size()==1){
            kk.setNumber(kks.get(0).getNumber()+1);
            kk.setId(kks.get(0).getId());
            shoppingCartMapper.updateNumberByID(kk);
        }
        else{
            //是菜品
            if(kk.getDishId()!=0){
                DishVO aDish = dishMapper.dishById(kk.getDishId());
                kk.setName(aDish.getName());
                kk.setImage(aDish.getImage());
                kk.setAmount(aDish.getPrice());

            }
            else {
                SetmealVO aSetmeal = setmealMapper.setmealById(kk.getSetmealId());
                kk.setName(aSetmeal.getName());
                kk.setImage(aSetmeal.getImage());
                kk.setAmount(aSetmeal.getPrice());

            }

            kk.setNumber(1);
            kk.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addToCart(kk);

        }


    }

    @Override
    public void cleanShopCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanAllCart(userId);
    }

    @Override
    public void subObjShoppingCart(ShoppingCartDTO kkk) {
        ShoppingCart kk = new ShoppingCart();
        BeanUtils.copyProperties(kkk,kk);
        List<ShoppingCart> theOne = shoppingCartMapper.list(kk);
        System.out.println(theOne);
        if(theOne != null && theOne.size()==1){
            kk = theOne.get(0);
            if(theOne.get(0).getNumber()>=2){
                kk.setNumber(theOne.get(0).getNumber()-1);
                shoppingCartMapper.updateNumberByID(kk);
            }
            else{
                shoppingCartMapper.subOne(kk);
            }
        }
    }

    @Override
    public List<ShoppingCart> getListShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> kks =  shoppingCartMapper.getCartlist(userId);
        return kks;
    }
}
